<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>菜单列表</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css">
    <link rel="stylesheet" href="../../../admin/css/dimples.css">
    <link rel="icon" href="../../../admin/images/favicon.ico" type="image/x-icon"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">菜单名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="menuName" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="menu-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="menu-table" lay-filter="menu-table"></table>
    </div>
</div>

<script type="text/html" id="menu-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" {{checkPerms('menu:add')}}>
    <i class="layui-icon layui-icon-add-1"></i>
    新增
    </button>
    <button class="pear-btn pear-btn-md" lay-event="batchRemove" {{checkPerms('menu:delete')}}>
    <i class="layui-icon layui-icon-delete"></i>
    删除
    </button>
</script>

<script type="text/html" id="menu-bar">
    <span {{noPerms('menu:delete','user:edit')}} >
    <span class="layui-badge-dot dimples-bg-orange"></span>
    无权限
    </span>

    <a lay-event="edit" {{checkPerms('menu:edit')}}>
    <i class="layui-icon layui-icon-set dimples-edit-area dimples-blue"></i>
    </a>
    </a>
    <a lay-event="remove" {{checkPerms('menu:delete')}}>
    <i class="layui-icon dimples-edit-area dimples-red">&#xe7f9;</i>
    </a>
</script>

<script type="text/html" id="menu-type">
    {{#if (d.type == '0') { }}
    <span>目录</span>
    {{# }else if(d.type == '1'){ }}
    <span>菜单</span>
    {{# }else if(d.type == '2'){ }}
    <span>按钮</span>
    {{# } }}
</script>

<!--<script type="text/html" id="menu-enable">
	<input type="checkbox" name="enable" value="{{d.menuId}}" lay-skin="switch" lay-text="启用|禁用"
				 lay-filter="menu-enable" {{ d.enable== true ? 'checked' : '' }}>
</script>-->

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>

</body>
<!-- 依 赖 脚 本 -->
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../component/pear/dimples.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'treetable', 'popup', 'ajax', 'loading', 'common', 'open'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let popup = layui.popup;
        let ajax = layui.ajax;
        let loading = layui.loading;
        let common = layui.common;
        let open = layui.open;

        let MODULE_PATH = BASE_URL + "/system/menu/";

        window.render = function () {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'menuId',
                treePidName: 'parentId',
                skin: 'line',
                method: 'get',
                treeDefaultClose: true,
                toolbar: '#menu-toolbar',
                elem: '#menu-table',
                url: BASE_URL + '/system/menu',
                page: false,
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'menuName', minWidth: 200, title: '菜单名称'},
                        {field: 'icon', title: '图标', templet: '#icon'},
                        {field: 'type', title: '权限类型', templet: '#menu-type'},
                        /*{field: 'enable', title: '是否可用', templet: '#menu-enable'},*/
                        {field: 'perms', title: '权限标识'},
                        {field: 'url', title: '路由'},
                        {field: 'orderNum', title: '排序'},
                        {field: 'menuId', minWidth: 100, title: '菜单编号', hide: true},
                        {title: '操作', templet: '#menu-bar', width: 150, align: 'center'}
                    ]
                ]
            });
        }

        render();

        form.on('submit(menu-query)', function (data) {
            var keyword = data.field.menuName;
            var $tds = $('#menu-table').next('.treeTable').find('.layui-table-body tbody tr td');
            if (!keyword) {
                $tds.css('background-color', 'transparent');
                layer.msg("请输入关键字", {icon: 5});
                return;
            }
            var searchCount = 0;
            $tds.each(function () {
                $(this).css('background-color', 'transparent');
                if ($(this).text().indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount == 0) {
                        let bodyHtml = $('body,html');
                        bodyHtml.stop(true);
                        bodyHtml.animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5});
            } else {
                treetable.expandAll('#menu-table');
            }
            return false;
        });

        table.on('tool(menu-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })

        table.on('toolbar(menu-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        /*form.on('switch(menu-enable)', function (obj) {
            let operate;
            if (obj.elem.checked) {
                operate = "enable";
            } else {
                operate = "disable";
            }
            let loading = layer.load();
            $.ajax({
                url: '/system/menu/' + operate,
                data: JSON.stringify({menuId: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.success) {
                        popup.success(result.msg);
                    } else {
                        popup.failure(result.msg);
                    }
                }
            })
        });*/


        window.add = function () {
            open.iframe('新增', 'add.html');
        }

        window.edit = function (obj) {
            open.iframe('修改', 'edit.html?menuId=' + obj.data['menuId']);
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该菜单', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                loading.Load(1, '');
                ajax.send('/system/menu/' + obj.data['menuId'], {
                    type: 'DELETE'
                }, function (result) {
                    popup.success(result.msg, function () {
                        obj.del();
                    })
                });
            });
        }
        window.batchRemove = function (obj) {
            let ids = common.checkField(obj, 'menuId');
            if (common.isEmpty(ids)) {
                popup.warning("未选中数据");
                return false;
            }
            layer.confirm('确定要删除选中菜单', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                loading.Load(5, '');
                ajax.send('/system/menu/batch/' + ids, {
                    type: 'delete'
                }, function (result) {
                    popup.success(result.msg, function () {
                        render().reload('menu-table');
                    })
                })
            });
        }

        window.refresh = function () {
            table.reload('menu-table');
        }
    })
</script>
</html>